package org.tuproj.drujbapizza;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {
	private EditText etUsername;
	private EditText etPassword;
	private EditText etConfirmPassword;
	private EditText etFirstname;
	private EditText etLastname;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		etUsername = (EditText) findViewById(R.id.etUsername);
		etPassword = (EditText) findViewById(R.id.etPassword);
		etConfirmPassword = (EditText) findViewById(R.id.etConfirmPassword);
		etFirstname = (EditText) findViewById(R.id.etFirstName);
		etLastname = (EditText) findViewById(R.id.etLastName);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_register, menu);
		return true;
	}

	public void registerUser(View v) {
		RegisterTask registerTask = new RegisterTask();
		registerTask
				.execute(new String[] { "http://10.0.2.2:8080/pizza-rest/rest/user/register" });
	}

	private class RegisterTask extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... params) {
			String usernameText = etUsername.getText().toString();
			String passwordText = etPassword.getText().toString();
			String confirmPasswordText = etConfirmPassword.getText().toString();
			String firstNameText = etFirstname.getText().toString();
			String lastnameText = etLastname.getText().toString();
			String result = "";
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(params[0]);

			if (confirmPasswordText.equals(passwordText)) {
				try {
					List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
					nameValuePairs.add(new BasicNameValuePair("name",
							firstNameText));
					nameValuePairs.add(new BasicNameValuePair("surname",
							lastnameText));
					nameValuePairs.add(new BasicNameValuePair("username",
							usernameText));
					nameValuePairs.add(new BasicNameValuePair("password",
							passwordText));
					nameValuePairs.add(new BasicNameValuePair(
							"password_repeat", confirmPasswordText));
					httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
					HttpResponse response = httpclient.execute(httppost);
					String s = Integer.valueOf(
							response.getStatusLine().getStatusCode())
							.toString();
					Log.i("Status for registering", s);
					result = s;
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				result = "Passwords don't match";
			}
			return result;
		}

		@Override
		protected void onPostExecute(String result) {
			if ("200".equals(result)) {
				Toast.makeText(RegisterActivity.this,
						"Registration succseful.Now you can login",
						Toast.LENGTH_LONG).show();
				startActivity(new Intent(RegisterActivity.this,LoadingActivity.class));	
			}
			else{ 
				Toast.makeText(RegisterActivity.this,
						"Registration fail.Please try again",
						Toast.LENGTH_LONG).show();
			}
		}
	};
}
